package com.neusoft.common.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.log4j.Logger;

public class WeixinConfigUtil {
	
	private static Properties pros = new Properties();
	private static final String FILEPATH = "/conf.properties";
	private static final Logger LOG = Logger.getLogger(WeixinConfigUtil.class);
	

	static{
		loadProperties();
	}
	
	private static void loadProperties(){
		try {
            InputStream is = WeixinConfigUtil.class.getResourceAsStream(FILEPATH);
            pros.load(is);
            if (is != null)
                is.close();
        } catch (Exception e) {
        	LOG.error(e + "没有发现配置文件，请将属性文件放入类路径");
        }
	}
	
	
	public static String getProperty(String key){
		String value = pros.getProperty(key);
		return value;
	}
	
	public static String getToken(){
		return pros.getProperty("TOKEN");
	}
	
	public static String getAppId(){
		return pros.getProperty("APPID");
	}
	public static String getAppSecret(){
		return pros.getProperty("APPSECRET");
	}
	
	public static String getProxyHost(){
		return pros.getProperty("PROXYHOST");
	}
	
	public static String getProxyPort(){
		return pros.getProperty("PROXYPORT");
	}
	
	public static String getURL(){
		return pros.getProperty("URL");
	}
	
	public static boolean putPropertys(Map<String,String> data){
		pros.putAll(data);
		return saveConfig();
	}
	
	public static Map getPropertys(){
		
		Map<String,String> map = new HashMap<String, String>();
		Set<Object> keys = pros.keySet();
		Iterator<Object> iterator = keys.iterator();
		while( iterator.hasNext() ){
			String key = (String) iterator.next();
			map.put(key, pros.getProperty(key));
		}
		return map;

	}


	private static boolean saveConfig() {
		URL url = WeixinConfigUtil.class.getResource(FILEPATH);
		File file = new File(url.getFile());
		System.out.println(file.getAbsolutePath());
		FileOutputStream fos = null;
		try {
			
			fos = new FileOutputStream(file);
			pros.store(fos, "");
			return true;
			
		} catch (Exception e) {
			LOG.error(e.getMessage());
			return false;
		}finally{
			if ( null != fos ){
				try {
					fos.close();
				} catch (IOException e) {
					LOG.error(e.getMessage());
				}
			}
		}
	}
	
	public static void removePros(){
		pros.clear();
		saveConfig();
	}


	public static void main(String[] args) throws Exception {
		Map<String,String> data = new HashMap<String, String>();
		data.put("test1", "test1");
		data.put("test2", "test2");
		WeixinConfigUtil.putPropertys(data);
		System.out.println(WeixinConfigUtil.getProperty("test1"));
		System.out.println(WeixinConfigUtil.getProperty("test2"));
		WeixinConfigUtil.removePros();
		
	}

}
